Skip to main content
Version: 1.0.0

catalogOrderRejectionNotification

Email Template​

Order Rejection Notification Template

Function Name: catalogOrderRejectionNotification

Author: Domenico Cerone Creation Date: 02/10/2025
Last Reviewer: Domenico Cerone

Trigger: HTTPS (onRequest)

Purpose: Sends order rejection notifications to Admin users when a catalog order is rejected. This Cloud Function retrieves order details, extracts rejection reasons from associated tickets, and notifies all Admin profiles via ZeptoMail with a professional email template.

Detailed Functionality​

1. INPUT VALIDATION AND PARAMETERS​

  • Required Parameter: catalogOrderId - ID of the document in 'CatalogOrders' collection
  • Optional Parameter: dryRun - If true, simulates sending without actually sending emails
  • Validates that catalogOrderId is provided and not empty
  • Supports both real email sending and dry-run testing mode

2. CATALOG ORDER RETRIEVAL​

  • Queries 'CatalogOrders' collection using the provided catalogOrderId
  • Validates that the order document exists
  • Extracts profile (email) and list_tickets_refs (array of ticket IDs)
  • Validates that both properties exist and list_tickets_refs is a non-empty array

3. TICKET DATA PROCESSING​

  • Takes the last element from list_tickets_refs array (most recent ticket ID)
  • Queries 'Tickets' collection using the ticket ID
  • Validates that the ticket document exists
  • Extracts messages array from ticket data
  • Takes the first message and extracts the text property as rejection reason
  • Handles cases where no message text is available

4. PROFILE INFORMATION RETRIEVAL​

  • Queries 'Profiles' collection using the profile email as document ID
  • Extracts firstName and lastName from profile data
  • Composes full name by combining firstName and lastName
  • Falls back to email address if profile doesn't exist or names are empty
  • Handles missing profile data gracefully

5. NOTIFICATION PREFERENCE CHECK​

  • Checks if the user has enabled the catalogOrderRejectionNotification in their notification preferences
  • Verifies notification_types.catalogOrderRejectionNotification field in the profile
  • If notification is disabled (false): returns success without sending email
  • If notification is enabled (true): proceeds with email sending
  • Provides clear logging about notification preference status

6. ADMIN RECIPIENTS COLLECTION​

  • Queries 'Profiles' collection for all documents with role = 'Admin'
  • Validates that at least one Admin profile exists
  • Extracts email addresses and names for each Admin
  • Prepares recipient list in ZeptoMail format with email and name

7. EMAIL TEMPLATE CONFIGURATION​

  • Uses ZeptoMail as the primary email service
  • Template Key: 13ef.8598f19fbcc5adb.k1.ee5f0400-9d0f-11f0-9d65-dad70ff08860.19994a14040
  • Sender: "no-reply@arshades.com" (ARShades Studio)
  • Professional template with order details and rejection reason

8. TEMPLATE VARIABLES​

  • {{catalogOrdersId}}: ID of the catalog order
  • {{profile}}: Full name of the profile (firstName lastName) instead of email
  • {{rejectionReason}}: Text message from the ticket in list_tickets_refs

9. DRY-RUN FUNCTIONALITY​

  • When dryRun = true: Simulates the entire process without sending emails
  • Returns all data that would have been sent
  • Useful for testing and validation
  • Logs all parameters that would be used for email sending

10. ERROR HANDLING AND VALIDATION​

  • Comprehensive validation at each step
  • Specific error messages for missing data
  • Graceful handling of missing profiles or tickets
  • Detailed logging for debugging purposes
  • Proper HTTP status codes for different error scenarios

11. RESPONSE STRUCTURE​

  • Success response includes all processed data
  • Admin recipient information
  • Template variables used
  • ZeptoMail result or dry-run simulation
  • Detailed error information on failure

Input (Payload)​

Method: POST

Headers:

  • Content-Type: application/json

Body:

{
"catalogOrderId": "tTIbV3A2WqtSm8BvrHTg",
"dryRun": false
}

Parameters:

  • catalogOrderId (string, required): Unique identifier of the catalog order document
  • dryRun (boolean, optional): If true, simulates sending without actually sending emails (default: false)

Output (Success)​

{
"success": true,
"message": "Notifica di rifiuto ordine catalogo inviata con successo agli Admin",
"details": {
"catalogOrderId": "tTIbV3A2WqtSm8BvrHTg",
"profileEmail": "user@example.com",
"profileFullName": "John Doe",
"lastTicketId": "ticket123",
"rejectionReason": "Prodotto non disponibile in magazzino",
"adminRecipientsCount": 3,
"adminEmails": [
"admin1@example.com",
"admin2@example.com",
"admin3@example.com"
],
"dryRun": false
},
"zeptomail_merge_info": {
"catalogOrdersId": "tTIbV3A2WqtSm8BvrHTg",
"profile": "John Doe",
"rejectionReason": "Prodotto non disponibile in magazzino"
},
"zeptomail_result": {
"data": {
"message": "Email sent successfully"
}
}
}

Response Properties:

  • success (boolean): Indicates if the operation was completed successfully
  • message (string): Descriptive message about the operation result
  • details (object): Detailed information about the processed data
  • zeptomail_merge_info (object): Template variables sent to ZeptoMail
  • zeptomail_result (object): Response from ZeptoMail service

Output (Error)​

{
"success": false,
"error": "Ordine catalogo con ID tTIbV3A2WqtSm8BvrHTg non trovato"
}

Common Error Scenarios:

  • Missing catalogOrderId parameter
  • Catalog order not found
  • Missing profile or list_tickets_refs properties
  • Ticket not found
  • No Admin profiles found
  • ZeptoMail service errors

Testing​

URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/catalogOrderRejectionNotification

Test with Emulator:

  1. Start Firebase emulators:

    firebase emulators:start --only functions
  2. Set Node.js version:

    nvm use 20
  3. Test with real email sending:

    curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/catalogOrderRejectionNotification" \
    -H "Content-Type: application/json" \
    -d '{"catalogOrderId": "tTIbV3A2WqtSm8BvrHTg"}'
  4. Test with dry-run (no email sent):

    curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/catalogOrderRejectionNotification" \
    -H "Content-Type: application/json" \
    -d '{"catalogOrderId": "tTIbV3A2WqtSm8BvrHTg", "dryRun": true}'

Postman Testing:

  • Method: POST
  • URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/catalogOrderRejectionNotification
  • Headers:
    • Key: Content-Type
    • Value: application/json
  • Body: raw JSON (see examples above)

Deploy Command​

firebase deploy --only functions:catalogOrderRejectionNotification

Production URL​

Live Function: https://europe-central2-arshades-7e18a.cloudfunctions.net/catalogOrderRejectionNotification

MAIL_TEMPLATE_KEY​

  • Order Rejection: 13ef.8598f19fbcc5adb.k1.ee5f0400-9d0f-11f0-9d65-dad70ff08860.19994a14040